const inc = (n) => n + 1;
console.log(inc(1));

// 箭头函数与 this
const person = {
  name: "tom",
  /* sayHi: function () {
    console.log(`hi, my name is ${this.name}`);
  }, */
  sayHi: () => {
    console.log(`hi, my name is ${this.name}`);
  },
  /* sayHiAsync: function () {
    // setTimeout的回调函数会被放到全局当中，因此this为undefined
    setTimeout(function () {
      console.log(this.name);
    });
  }, */

  // 解决方法1：闭包
  sayHiAsync: function () {
    const that = this;
    setTimeout(function () {
      console.log(that.name);
    });
  },

  // 解决方法2：箭头函数
  sayHiAsync: function () {
    setTimeout(() => {
      console.log(this.name);
    });
  },
};

person.sayHi();
person.sayHiAsync();
